/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
#include <rtcArg.h>
#include <typecomm.h>
#include <dateopt.h>
/*
    read time reference:
    sudo ./builddir/test -r -f /dev/rtc0 -c /tmp/tmzonesave
    write time reference:
    sudo ./builddir/test -w "2024-09-14 13:33:00" -f /dev/rtc0 -c /tmp/tmzonesave -z 8
*/
int main(int argc, char **argv)
{
    rtcParseInArg(argc, argv);
    if (rtcArgInput.read)
    {
        if (!(strlen(rtcArgInput.rtcfile) > 0 && strlen(rtcArgInput.zonefile) > 0))
        {
            printf("read date error, need rtcfile and zonfile\n");
            return -1;
        }
        tmSdInit(rtcArgInput.rtcfile, rtcArgInput.zonefile);
        tmdateType tmpdate = tmSdGetDate();
        printf("date: %d-%02d-%02d %02d:%02d:%02d; week: %d; zone: %d\n", tmpdate.year, tmpdate.month, tmpdate.day,
               tmpdate.hour, tmpdate.minute, tmpdate.second, tmpdate.week, tmpdate.cUTC);
#if __WORDSIZE == 64
        printf("stamp sec: %lu\n", tmSdGetStampSec());
        printf("stamp ms: %lu\n", tmSdGetStampMs());
#else
        printf("stamp sec: %llu\n", tmSdGetStampSec());
        printf("stamp ms: %llu\n", tmSdGetStampMs());
#endif
        // if(sizeof(long) == 4)// 32bit
        // {
        //     printf("stamp sec: %llu\n", tmSdGetStampSec());
        //     printf("stamp ms: %llu\n", tmSdGetStampMs());
        // }
        // else if(sizeof(long) == 8)//64bit
        // {
        //     printf("stamp sec: %lu\n", tmSdGetStampSec());
        //     printf("stamp ms: %lu\n", tmSdGetStampMs());
        // }
    }
    else if (strlen(rtcArgInput.write) > 0)
    {
        if (!(strlen(rtcArgInput.rtcfile) > 0 && strlen(rtcArgInput.zonefile) > 0))
        {
            printf("write date error, need rtcfile and zonfile\n");
            return -1;
        }
        printf("write time zone=%d\n", rtcArgInput.zone);
        if (!(rtcArgInput.zone >= -11 && rtcArgInput.zone <= 11))
        {
            printf("write date error, need config zone\n");
            return -1;
        }
        tmSdInit(rtcArgInput.rtcfile, rtcArgInput.zonefile);
        tmdateType tmpdate;
        int tmpbuf[6];
        if (sscanf(rtcArgInput.write, "%d-%d-%d %d:%d:%d", &tmpbuf[0], &tmpbuf[1], &tmpbuf[2], &tmpbuf[3], &tmpbuf[4], &tmpbuf[5]) != 6)
        {
            printf("write date format error:%s\n", rtcArgInput.write);
            return -1;
        }
        tmpdate.year = tmpbuf[0];
        tmpdate.month = tmpbuf[1];
        tmpdate.day = tmpbuf[2];
        tmpdate.hour = tmpbuf[3];
        tmpdate.minute = tmpbuf[4];
        tmpdate.second = tmpbuf[5];
        // check input val ok:

        tmpdate.cUTC = rtcArgInput.zone;
        tmSdSetDate(&tmpdate);
        printf("config time ok:%s\n", rtcArgInput.write);
    }
    else if (rtcArgInput.optzone >= -11 && rtcArgInput.optzone <= 11)
    {
        if (!(strlen(rtcArgInput.rtcfile) > 0 && strlen(rtcArgInput.zonefile) > 0))
        {
            printf("config zone error, need rtcfile and zonfile\n");
            return -1;
        }
        tmSdInit(rtcArgInput.rtcfile, rtcArgInput.zonefile);
        tmSdSetUtc(rtcArgInput.optzone);
        printf("config zone ok:%d\n", rtcArgInput.optzone);
    }
    else
    {
        printfRtcArgs();
        return 1;
    }
    return 1;
}